package com.clarence.dada.modular.system.menu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clarence.dada.core.base.PageResult;
import com.clarence.dada.core.enums.ErrorEnum;
import com.clarence.dada.core.enums.YesOrNotEnum;
import com.clarence.dada.core.exception.BusException;
import com.clarence.dada.core.factory.PageFactory;
import com.clarence.dada.core.factory.PageResultFactory;
import com.clarence.dada.modular.system.menu.entity.SysMenuButtonEntity;
import com.clarence.dada.modular.system.menu.entity.SysMenuEntity;
import com.clarence.dada.modular.system.menu.entity.req.SysMenuButtonReq;
import com.clarence.dada.modular.system.menu.entity.resp.SysMenuButtonResp;
import com.clarence.dada.modular.system.menu.factory.MenuButtonFactory;
import com.clarence.dada.modular.system.menu.mapper.SysMenuButtonMapper;
import com.clarence.dada.modular.system.menu.service.SysMenuButtonService;
import com.clarence.dada.modular.system.menu.service.SysMenuService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统菜单按钮service接口实现类
 *
 * @author GD
 * @since 2021/1/9 11:05
 */
@Service
public class SysMenuButtonServiceImpl extends ServiceImpl<SysMenuButtonMapper, SysMenuButtonEntity> implements SysMenuButtonService {

    @Lazy
    @Resource
    private SysMenuService sysMenuService;


    @Override
    public void menuButtonAdd(SysMenuButtonReq req) {
        SysMenuButtonEntity sysMenuButtonEntity = Convert.convert(SysMenuButtonEntity.class, req);
        this.save(sysMenuButtonEntity);
    }

    @Override
    public void addDefaultButtons(SysMenuButtonReq sysMenuButtonReq) {
        SysMenuEntity sysMenuEntity = this.sysMenuService.getById(sysMenuButtonReq.getMenuId());
        // 构建菜单的系统默认按钮
        List<SysMenuButtonEntity> sysMenuButtonEntityList = MenuButtonFactory.createSystemDefaultButton(sysMenuEntity.getMenuId(), sysMenuEntity.getMenuName(), sysMenuEntity.getMenuCode());
        this.saveBatch(sysMenuButtonEntityList);
    }

    @Override
    public void menuButtonBatchDelete(SysMenuButtonReq req) {
        if (ArrayUtil.isNotEmpty(req.getButtonIds())) {
            LambdaUpdateWrapper<SysMenuButtonEntity> wrapper = new LambdaUpdateWrapper<>();
            wrapper.in(SysMenuButtonEntity::getButtonId, req.getButtonIds())
                    .eq(SysMenuButtonEntity::getDelFlag, YesOrNotEnum.N.getCode())
                    .set(SysMenuButtonEntity::getDelFlag, YesOrNotEnum.Y.getCode());
            this.update(wrapper);
        }
    }

    @Override
    public void menuButtonEdit(SysMenuButtonReq req) {
        SysMenuButtonEntity button = this.getById(req.getButtonId());
        if (ObjectUtil.isNull(button)) {
            throw new BusException(ErrorEnum.MENU_BUTTON_NOT_EXIST);
        }
        BeanUtil.copyProperties(req, button);
        button.setDelFlag(null);
        button.setMenuId(null);
        button.setButtonCode(null);
        this.updateById(button);
    }

    @Override
    public SysMenuButtonResp menuButtonDetail(SysMenuButtonReq sysMenuButtonReq) {
        LambdaQueryWrapper<SysMenuButtonEntity> queryWrapper = this.createWrapper(sysMenuButtonReq);
        SysMenuButtonEntity sysMenuButtonEntity = this.getOne(queryWrapper, false);
        return Convert.convert(SysMenuButtonResp.class, sysMenuButtonEntity);
    }

    @Override
    public PageResult<SysMenuButtonResp> menuButtonPage(SysMenuButtonReq req) {
        LambdaQueryWrapper<SysMenuButtonEntity> wrapper = this.createWrapper(req);
        Page<SysMenuButtonEntity> page = this.page(PageFactory.defaultPage(), wrapper);
        List<SysMenuButtonResp> respList = Convert.convert(new TypeReference<>() {}, page.getRecords());
        return PageResultFactory.result(page,respList);
    }


    @Override
    public void menuButtonDeleteByMenuId(Long menuId) {
        if (ObjectUtil.isNotEmpty(menuId)) {
            LambdaUpdateWrapper<SysMenuButtonEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SysMenuButtonEntity::getMenuId, menuId)
                    .eq(SysMenuButtonEntity::getDelFlag, YesOrNotEnum.N.getCode())
                    .set(SysMenuButtonEntity::getDelFlag, YesOrNotEnum.Y.getCode());
            this.update(updateWrapper);
        }
    }

    /**
     * 实体构建queryWrapper
     *
     * @author GD
     * @since 2021/1/24 22:03
     */
    private LambdaQueryWrapper<SysMenuButtonEntity> createWrapper(SysMenuButtonReq req) {
        LambdaQueryWrapper<SysMenuButtonEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenuButtonEntity::getDelFlag, YesOrNotEnum.N.getCode());

        if (ObjectUtil.isEmpty(req)) {
            return wrapper;
        }
        wrapper.eq(ObjectUtil.isNotEmpty(req.getButtonId()), SysMenuButtonEntity::getButtonId, req.getButtonId())
                .eq(ObjectUtil.isNotEmpty(req.getMenuId()), SysMenuButtonEntity::getMenuId, req.getMenuId())
                .like(ObjectUtil.isNotEmpty(req.getButtonName()), SysMenuButtonEntity::getButtonName, req.getButtonName())
                .like(ObjectUtil.isNotEmpty(req.getButtonCode()), SysMenuButtonEntity::getButtonCode, req.getButtonCode());
        return wrapper;
    }

}
